/*
 * Authors: Alex Attar & Kevin El Hage
 * ID: 9765727
 * Date: 9/3/2012
 * 
 */

package taskManagerSystem.ControllerSubsystem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Component;
import java.io.File;

import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import taskManagerSystem.ModelSubsystem.Model;
import taskManagerSystem.ViewSubsystem.View;

/*
 * The Listener class handles events for all of the Swing components that
 * trigger an ActionEvent and register an ActionListener 
 */
public class Listener implements ActionListener{	
	
	private Model model;
	private View view;
	
	/*
	 * The constructor receives references to the model and view so that
	 * they can be updated as needed. 
	 */
	public Listener(Model model, View view) {
		this.model = model;
		this.view = view;
	}

	@Override
	/*
	 * actionPerformed is called by any component for which the Listener is registered
	 * when an event is triggered. It determines what called the event and acts appropriately.
	 */
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		
		if (actionCommand.equals("New")){
			newEvent();
		}
		if (actionCommand.equals("Load")){
			loadFileEvent();
		}
		if (actionCommand.equals("Save")){
			saveFileEvent();
		}
		if (actionCommand.equals("Exit")){
			exitEvent(e);
		}
		if (actionCommand.equals("New task")){
			newTaskEvent();
		}
		if (actionCommand.equals("Delete task")){
			delTaskEvent();
		}
	}
	
	/*
	 * newEvent is performed when "New" is chosen from the "File" menu
	 * The model is informed that it must clear its data and the view is
	 * informed that it must clear its tables.
	 */
	private void newEvent(){
		model.clearData();
		view.emptyPersonTable();
		view.emptyTaskTable();
	}
	
	
	/*
	 * loadFileEvent is performed when "Load" is chosen from the "File" menu
	 * A dialog allows the user to select a file, then the model is informed
	 * that it must load the contents of that file.
	 * The view is then told to update to reflect the new model.
	 */
	private void loadFileEvent(){
		JFileChooser fileopen = new JFileChooser();
		FileFilter filter = new FileNameExtensionFilter("XML Tasks file", "xml");
		fileopen.addChoosableFileFilter(filter);

		int ret = fileopen.showOpenDialog(null);

		if (ret == JFileChooser.APPROVE_OPTION){
			File file = fileopen.getSelectedFile();
			String filename = file.getPath();
			model.loadTasks(filename);
			model.buildPeopleFromTasks();
			view.populateTaskTable();
			view.populatePersonTable();
		}
	}
	
	/*
	 * saveFileEvent is performed when "Save" is selected from the "File" menu
	 * A dialog allows the user to select which file to save to, then the model
	 * is informed that it must output its data to the chosen file.
	 */
	private void saveFileEvent(){
		JFileChooser filesave = new JFileChooser();
		FileFilter filter = new FileNameExtensionFilter("XML Tasks file", "xml");
		filesave.addChoosableFileFilter(filter);
		
		int ret = filesave.showSaveDialog(null);
		
		if (ret == JFileChooser.APPROVE_OPTION){
			File file = filesave.getSelectedFile();
			String filename = file.getPath();
			model.saveData(filename);
		}
	}

	/*
	 * exitEvent is performed when "Exit" is selected from the "File" menu.
	 * The model is queried to determine if any changes have been made since
	 * the last save. If so, a dialog appears asking the user if they want to
	 * save the changes before exiting. The system then closes. 
	 */
	private void exitEvent(ActionEvent arg0){
		if(model.hasChanged()) {
			int option = JOptionPane.showOptionDialog((Component) arg0.getSource(), 
					"Do you want to save before exiting?", 
					"Save On Exit", JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.WARNING_MESSAGE, null, null, null);

			//if user clicks on "Yes", changes are saved to the file and
			//the program exits. Else if user clicks on "No", the system 
			//exits without modifying the file
			if(option == JOptionPane.YES_OPTION) {
				JFileChooser filesave = new JFileChooser();
				FileFilter filter = new FileNameExtensionFilter("XML Tasks file", "xml");
				filesave.addChoosableFileFilter(filter);

				int ret = filesave.showSaveDialog(null);

				if (ret == JFileChooser.APPROVE_OPTION){
					File file = filesave.getSelectedFile();
					String filename = file.getPath();
					model.saveData(filename);
					System.exit(0);
				}
			} else if(option == JOptionPane.NO_OPTION) {
				System.exit(0);
			}
		}else  if(model.hasChanged() == false){
			System.exit(0);
		}
	}

	/*
	 * newTaskEvent is performed when "New task" is selected from the "Edit" menu.
	 * The model is informed that a new, blank task is to be added. The view is then
	 * informed that it must update its tables.  
	 */
	private void newTaskEvent(){
		model.addTask();
		view.populateTaskTable();
		view.populatePersonTable();		
	}
	
	/* 
	 * delTaskEvent is performed when "Delete task" is selected from the "Edit" menu.
	 * The view must be queried to find exactly which row, if any, is selected and then
	 * the relevant task's ID is queried. The model is informed of the ID of the task to
	 * be removed, then the view is informed that it must update. 
	 */
	private void delTaskEvent(){
		int index = view.getSelectedRow();
		if (index >= 0){
			int taskID = view.getSelectedTaskID();
			model.removeTask(taskID);
			view.populateTaskTable();
			view.populatePersonTable();
		}		
	}

}

